import express from 'express';
import * as users from "../../controllers/users.js"
import * as categories from "../../controllers/categories.js"
import * as articles from "../../controllers/articles.js"
import * as comments from "../../controllers/comments.js"
import initUpload from "../../util/upload.js"

const upload = initUpload();

const router = express.Router();

//2
router.get("/info", async function (req, res) {
    let data = await users.selectUsers();
    res.send({
        code: 200,
        msg: "恭喜啊，兄弟，用户信息获取成功😀",
        data
    })
})

//3
router.get('/detail', async function (req, res) {
    let data = await users.selectUsersAll();
    res.send({
        code: 200,
        msg: "恭喜啊，兄弟，用户详情获取成功😀",
        data
    })
})

//4
// router.post('/user/edit', async function (req, res) { 

// })

//5
router.get('/category/list', async function (req, res) {
    let data = await categories.selectCategoriesClass();
    res.send({
        code: 200,
        msg: "恭喜啊，兄弟，所有文章类别获取成功😀",
        data
    })
})

//6
router.post("/category/add", async function (req, res) {
    let name = req.body?.name ?? "";
    let slug = req.body?.slug ?? "";
    if (name == "" || slug == "") {
        res.send({
            code: 400,
            msg: "请填写完整的信息",
        })
        return;
    }
    await categories.addCategories(name, slug);
    res.send({
        code: 200,
        msg: "恭喜恭喜，兄弟，添加成功😀"
    })
})

//7
router.get('/category/search', async (req, res) => {
    const id = req.query.id;
    if (id == null || id == "") {
        res.send({
            code: 400,
            msg: `参数错误，请求参数为 ${req.id}`
        })
        return;
    };
    let data = await categories.selectCategoriesClassId(id);
    res.send({
        code: 200,
        msg: "查询成功",
        data
    })
});

//8
router.post("/category/edit", async function (req, res) {
    let id = req.body?.id ?? "";
    let name = req.body?.name ?? "";
    let slug = req.body?.slug ?? "";
    if (id == "" || name == "" || slug == "") {
        res.send({
            code: 400,
            msg: "兄弟，上传参数不对啊QAQ"
        })
        return;
    }
    await categories.updateCategories(id, name, slug);
    res.send({
        code: 200,
        msg: "恭喜啊，兄弟，编辑成功😀"
    })
});

//9
router.post("/category/delete", async function (req, res) {
    let id = req.body?.id ?? "";
    if (id == "") {
        res.send({
            code: 400,
            msg: "兄弟，这个id不太对劲哦O.O"
        })
        return;
    }

    let [data] = await categories.selectCategoriesId(id);
    if (!data) {
        res.send({
            code: 400,
            msg: `兄弟，没有id为${id}的结果哦O.O`
        })
        return;
    }
    // if(data.state == 1){
    //     res.send({
    //         code: 400,
    //         msg: "兄弟，这个分类正在使用中哦O.O"
    //     })
    //     return;
    // }
    let list = await articles.selectArticlesCateId(id);
    if (list.length > 0) {
        res.send({
            code: 200,
            msg: "兄弟，这个分类下有文章哦O.O"
        })
        return;
    }

    await categories.deletecategories(id);
    res.send({
        code: 204,
        msg: "恭喜啊，兄弟，删除成功😀"
    })
})

//11
router.post("/article/publish", upload.single("cover") ,async (req,res)=>{
    let title = req.body.title;
    let cover = req.file.path;
    let categoryId = req.body.categoryId;
    let date = req.body.date;
    let content = req.body.content;
    let state = req.body.state;
    
    res.send({
        code:200,
        msg:"文章发布成功"
    });
})

//12
router.get('/article/search', async function (req, res) {
    let id = req.query.id;
    if (id == null || id == "") {
        res.send({
            code: 400,
            msg: `参数错误，请求参数为 ${req.id}`
        })
        return;
    };
    let [data] = await articles.selectArticlesId(id);
    res.send({
        code: 200,
        msg: "查询成功",
        data
    })
})

//14
router.post('/article/delete', async function (req, res) {
    let id = req.body?.id ?? "";
    if (id == null || id == "") {
        res.send({
            code: 400,
            msg: `参数错误，请求参数为 ${req.id}`
        })
        return;
    };
    await articles.deleteArticle(id);
    res.send({
        code: 200,
        msg: "删除成功",
    })
})

//15
router.get('/data/info',async function (req, res) { 
    let data = await articles.selectAllArticlesComments();
    res.send({
        code: 200,
        msg: "获取成功",
        data
    })
})

//16
router.get('/data/article', async function (req, res) {
    let data = await articles.selectArticlesNewContent();
    res.send({
        code: 200,
        msg: "查询成功",
        data
    })
})

//17
router.get('/data/category', async function (req, res) {
    let data = await articles.selectArticlesCategory();
    res.send({
        code: 200,
        msg: "查询成功",
        data
    })
})

//18
router.get('/data/visit', async function (req, res) {
    let data = await articles.selectArticlesDatecontent();
    res.send({
        code: 200,
        msg: "日访问量统计数据获取成功",
        data
    })
})

//19
router.get('/comment/search', async function (req, res) {
    let page = req.query.page;
    let perpage = req.query.perpage;
    console.log(page, perpage);
    console.log(req.query);

    if (page == null || page == "") {
        page = 1;
    }
    if (perpage == null || perpage == "") {
        perpage = 6;
    }
    let startindex = (page - 1) * perpage;
    let data = await articles.selectArticlesByPage(startindex, perpage);

    for (let articles of data) {
        let str = articles.content.replace(/<\/?[^>]+(>|$)/g, "").substring(0, 500);
        articles.content = str;
    }

    res.send({
        code: 200,
        mas: "恭喜啊，兄弟，文章评论搜索获取成功😀",
        data
    })
})

//20
router.post('/comment/pass', async (req, res) => {
    let id = req.body?.id ?? "";
    if (id == null || id == "") {
        res.send({
            code: 400,
            msg: `参数错误，请求参数为 ${req.id}`
        })
        return;
    };
    await comments.updateCommentPass(id);
    res.send({
        code: 200,
        msg: "操作成功",
    })
})

//21
router.post('/comment/reject', async (req, res) => {
    let id = req.body?.id ?? "";
    if (id == null || id == "") {
        res.send({
            code: 400,
            msg: `参数错误，请求参数为 ${req.id}`
        })
        return;
    };
    await comments.updateCommentReject(id);
    res.send({
        code: 200,
        msg: "操作成功",
    })
})

//22
router.post('/comment/delete', async (req, res) => {
    let id = req.body?.id ?? "";
    if (id == null || id == "") {
        res.send({
            code: 400,
            msg: `删除失败，请求参数为 ${req.id}`
        })
        return;
    };
    await comments.deleteComment(id);
    res.send({
        code: 200,
        msg: "删除成功"
    })
});


export default router;